package com.nanosn.util.parsers.propertiesfile;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/*
 *  Copyright 2009 selkhateeb.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
/**
 *
 * @author selkhateeb
 */
public class PropertiesInvocationHandler implements InvocationHandler {

    private File SettingsFile;
    private Properties properties = new Properties();

    public PropertiesInvocationHandler(String fileName) throws FileNotFoundException, IOException {
        this.SettingsFile = new File(fileName);
        this.properties.load(new FileInputStream(this.SettingsFile));
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        Property property = method.getAnnotation(Property.class);
        if (property == null) {
            throw new Exception("Property Annotation not defined for method " +
                                method.getName());
        }
        String value = properties.getProperty(property.name());

        Object returned;
        final Class<?> type = method.getReturnType();
        final List<Object> result = new ArrayList<Object>();

        if (type.isEnum()) {
            result.add(Enum.valueOf((Class<? extends Enum>) type, value));
        }
        else if (type.isPrimitive()) {
            if (type.equals(Character.TYPE) || type.equals(Character.class)) {
                result.add(value.charAt(0));
            }
            else if (type.equals(Byte.TYPE) || type.equals(Short.class)) {
                result.add(Byte.parseByte(value));
            }
            else if (type.equals(Short.TYPE) || type.equals(Short.class)) {
                result.add(Short.parseShort(value));
            }
            else if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
                result.add(Integer.parseInt(value));
            }
            else if (type.equals(Long.TYPE) || type.equals(Long.class)) {
                result.add(Long.parseLong(value));
            }
            else if (type.equals(Float.TYPE) || type.equals(Float.class)) {
                result.add(Float.parseFloat(value));
            }
            else if (type.equals(Double.TYPE) || type.equals(Double.class)) {
                result.add(Double.parseDouble(value));
            }
            else {
                throw new UnsupportedOperationException(String.format("Method (%s) return type not supported for reading argument values", type));
            }
        }
        else if (type.equals(Character.class)) {
            // there is always an exception, and java.lang.Character can't constructed from a string
            if (value.length() == 1) {
                result.add(value.charAt(0));
            }
            else {
                throw new Exception(String.format("value is not a character (%s)", value));
            }
        }
        else {
            final Constructor<?> constructor = type.getConstructor(new Class[]{String.class});
            result.add(constructor.newInstance(new Object[]{value}));
        }

//TODO
//            if (property.isMultiValued()) {
//                return result;
//            }
//            else {
        return result.get(0);
//            }

    }
}
